package com.hongxun.modules.bss.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hongxun.modules.csg.entity.*;
import com.hongxun.modules.csg.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongxun.modules.bss.entity.DPictureEntity;
import com.hongxun.modules.bss.service.DPictureService;
import com.hongxun.common.utils.PageUtils;
import com.hongxun.common.utils.Query;
import com.hongxun.common.utils.R;




/**
 * 现场照片信息表
 * 
 * @author zkcn
 * @email youeremail@163.com
 * @date 2018-09-06 16:29:07
 */
@RestController
@RequestMapping("/bss/dpicture")
public class DPictureController {
	@Autowired
	private DPictureService dPictureService;
	@Autowired
	private CScheduleReportService cScheduleReportService;
	@Autowired
	private CBasicsWorkService cBasicsWorkService;
	@Autowired
	private CGroundService cGroundService;
	@Autowired
	private CLinePitService cLinePitService;
	@Autowired
	private CPolePitService cPolePitService;


	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        String wheres =" 1=1 ";
		if(query.get("pGuid")!=null&&!query.get("pGuid").toString().equals(""))
		{
			wheres+=" and  p_guid = '" + query.get("pGuid").toString()+"' ";
		}
		if(query.get("dpGuid")!=null&&!query.get("dpGuid").toString().equals(""))
		{
			wheres+=" and  p_p_guid = '" + query.get("dpGuid").toString()+"' ";
		}
		if(query.get("pStatus")!=null&&!query.get("pStatus").toString().equals(""))
		{
			wheres+=" and  p_status = '" + query.get("pStatus").toString()+"' ";
		}
		if(query.get("pLabel")!=null && !query.get("pLabel").toString().equals("")){
			wheres+=" and  p_label like'%" + query.get("pLabel").toString()+"%' ";
		}
		//进度照片
		if(query.get("sGuid")!=null && !query.get("sGuid").toString().equals("")){
			String sGuid = params.get("sGuid").toString();
			String whereCSchedule = " 1=1 and s_guid='"+sGuid+"'";
			Map<String , Object> mapCSchedule = new HashMap<>();
			mapCSchedule.put("filterSql",whereCSchedule);
			List<CScheduleReportEntity> cScheduleReportList = cScheduleReportService.queryList(mapCSchedule);
			if(cScheduleReportList!=null && cScheduleReportList.size()!=0){
				for (int i = 0; i < cScheduleReportList.size(); i++) {
					if (i == 0) {
						wheres+=" and  (p_p_guid = '" + cScheduleReportList.get(i).getGuid()+"' ";
					}else{
						wheres+=" or  p_p_guid = '" + cScheduleReportList.get(i).getGuid()+"' ";
					}
				}
				wheres += " )";

			}else {
				wheres += " and 1=2 ";
			}
		}
		//中间检查项照片
		if(query.get("mcGuid")!=null && !query.get("mcGuid").toString().equals("")){
			String mcGuid = params.get("mcGuid").toString();
			String whereCSchedule = " 1=1 and m_c_guid='"+mcGuid+"'";
			Map<String , Object> mapCSchedule = new HashMap<>();
			mapCSchedule.put("filterSql",whereCSchedule);
			List<CBasicsWorkEntity> cBasicsWorkList = cBasicsWorkService.queryList(mapCSchedule);
			List<CGroundEntity> cGroundList = cGroundService.queryList(mapCSchedule);
			List<CLinePitEntity> cLineList = cLinePitService.queryList(mapCSchedule);
			List<CPolePitEntity> cPolePitList = cPolePitService.queryList(mapCSchedule);

			wheres+=" and p_p_guid  in ( ";
			String whereNew = "";
			if (cBasicsWorkList != null && cBasicsWorkList.size()!=0) {
				for (int i = 0; i < cBasicsWorkList.size(); i++) {
					whereNew += "  '" + cBasicsWorkList.get(i).getGuid()+"', ";
				}
			}

			if (cGroundList != null && cGroundList.size()!=0) {
				for (int i = 0; i < cGroundList.size(); i++) {
					whereNew += "  '" + cGroundList.get(i).getGuid()+"', ";
				}
			}

			if (cLineList != null && cLineList.size()!=0) {
				for (int i = 0; i < cLineList.size(); i++) {
					whereNew += "  '" + cLineList.get(i).getGuid()+"', ";
				}
			}

			if (cPolePitList != null && cPolePitList.size()!=0) {
				for (int i = 0; i < cPolePitList.size(); i++) {
					whereNew += "  '" + cPolePitList.get(i).getGuid()+"', ";
				}
			}

			wheres += whereNew.substring(0,whereNew.length()-2);
			wheres += " )";
		}

		wheres+=" and  p_datastate != '删除' ";
        query.put("filterSql",wheres+" ");
		List<DPictureEntity> dPictureList = dPictureService.queryList(query);
		int total = dPictureService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(dPictureList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{guid}")
	public R info(@PathVariable("guid") String guid){
		DPictureEntity dPicture = dPictureService.queryObject(guid);
		
		return R.ok().put("dPicture", dPicture);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody DPictureEntity dPicture){
		dPictureService.save(dPicture);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody DPictureEntity dPicture){
		dPictureService.update(dPicture);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] guids){
		dPictureService.deleteBatch(guids);
		
		return R.ok();
	}
	
}
